class Solution {
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int len1 = nums1.length;
        int len2 = nums2.length;
        int len = len1 + len2;
        if (len1 > len2) {
            return findMedianSortedArrays(nums2, nums1);
        }
        if (len1 == 0) {
            if (len2 % 2 != 0) {
                return nums2[len2 / 2];
            } else {
                return (nums2[len2 / 2 - 1] + nums2[len2 / 2]) / 2.0;
            }
        }
        int l = 0;
        int r = len1;
        while (l <= r) {
            int cur1 = l + (r - l) / 2;
            int cur2 = (len + 1) / 2 - cur1;
            int L1, R1, L2, R2;
            L1 = cur1 == 0 ? Integer.MIN_VALUE : nums1[cur1 - 1];
            R1 = cur1 == len1 ? Integer.MAX_VALUE : nums1[cur1];
            L2 = cur2 == 0 ? Integer.MIN_VALUE : nums2[cur2 - 1];
            R2 = cur2 == len2 ? Integer.MAX_VALUE : nums2[cur2];
            if (R1 < L2) {
                l = cur1 + 1;
            } else if (L1 > R2) {
                r = cur1 - 1;
            } else {
                if (len % 2 != 0) {
                    return Math.max(L1, L2);
                } else {
                    return (Math.max(L1, L2) + Math.min(R1, R2)) / 2.0;
                }
            }
        }
        return 0;
    }
}